package com.leji.song.mainPage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.dgl.sdk.util.JsonUtil;
import com.dgl.sdk.view.PullListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.leji.song.R;
import com.leji.song.extra.BaseActivity;
import com.leji.song.extra.BaseCoreActivity;
import com.leji.song.httpInterface.BaseAjaxParams;
import com.leji.song.httpInterface.HttpInterface;
import com.leji.song.httpInterface.NomalResult;
import com.leji.song.mainPage.model.DS_Kouwei;
import com.leji.song.mainPage.model.DS_ShopDetail;
import com.leji.song.mainPage.model.DS_ShopInfo;
import com.leji.song.mine.AdressManagerActivity;
import com.leji.song.util.BaiduMapUtil;
import com.leji.song.util.ConstAns;
import com.leji.song.util.MyAjaxCallBack;
import net.tsz.afinal.FinalHttp;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by colin.ding on 2014/6/4.
 */
public class BuyFoodActivity extends BaseActivity{
    private PullListView shopListView;
    private PopupWindow kouweiPopupWindow;
    private PopupWindow nearPopupWindow;

    private TextView myAdress;

//    private List<DS_Kouwei> kouWeiList = new ArrayList<DS_Kouwei>();
    List<HashMap<String, String>> kouWeiMapList =  new ArrayList<HashMap<String, String>>();
    private SimpleAdapter kouweiAdapter;
    private List<DS_Kouwei>  kouWeiList;
    private GridView nearGridView;

    private View locationView;

    private BroadcastReceiver broadcastReceiver;
    private List<DS_ShopDetail> shopList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setAnimType(RIGHT_ENTER_LEFT_EXT);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.food_sell);

        initView();

        initKouWeiPopupwindow();
        initNearPopupwindow();

        getShopList(null, null);
        getKouWeiList();

        getLocationAddress();
    }



    private void initView() {
        setReturnBtn();
        setActivityTitle("外卖");
        setRightBtn(R.drawable.search_icon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BuyFoodActivity.this, SearchShopActivity.class);
                intent.putExtra("shopList", (Serializable)shopList);
                startActivity(intent);
            }
        });

        shopListView = (PullListView) findViewById(R.id.shopListView);
        myAdress = (TextView) findViewById(R.id.myAdress);
        locationView = (View) findViewById(R.id.locationView);

        if(!BaiduMapUtil.myAdress.equals("")){
            myAdress.setText(BaiduMapUtil.myAdress);
        }

        locationView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showActivity(AdressManagerActivity.class);
            }
        });
    }

    private void  getLocationAddress() {
//        BaiduMapUtil baiduMap = new BaiduMapUtil(this, new BaiduMapUtil.GetLocationCallback() {
//            @Override
//            public void onLocation(String locationString) {
//                myAdress.setText(locationString);
//            }
//        });
//
//        baiduMap.start();

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                String locationString = intent.getStringExtra("location");
                if((locationString!= null) && (!locationString.equals(""))){
                    myAdress.setText(locationString);
                }
            }
        };

        registerReceiver(broadcastReceiver, new IntentFilter(ConstAns.MSG_LOCATION_CHANGE));
    }


    private void getKouWeiList() {

        BaseAjaxParams params = new BaseAjaxParams();

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetkouWeiList.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o,BuyFoodActivity.this);
                if (obj != null) {

                    try{
                        Gson gson = new Gson();
                        Type listType = new TypeToken<List<DS_Kouwei>>() {
                        }.getType();
                        kouWeiList = gson.fromJson(gson.toJson(obj), listType);

                        if(kouWeiList != null){
                            for(DS_Kouwei kouwei: kouWeiList){
                                HashMap<String, String> map = new HashMap<String, String>();
                                map.put("id", kouwei.getId());
                                map.put("name", kouwei.getName());

                                kouWeiMapList.add(map);
                            }

                            kouweiAdapter.notifyDataSetChanged();
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }

            }
        });



    }



    private void getShopList(String type, String distance) {
        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetShop.params.size, "20");
        if(type != null){
            params.put(HttpInterface.HIF_GetShop.params.type, type);
        }

        if(distance != null){
            params.put(HttpInterface.HIF_GetShop.params.distance, distance);
        }

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetShop.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity) this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o, BuyFoodActivity.this);
                if (obj != null) {

                    try{
                        Gson gson = new Gson();

                        DS_ShopInfo shopInfo = JsonUtil.fromJson(gson.toJson(obj), DS_ShopInfo.class);

                        shopList = shopInfo.getDatas();

                        ShopAdapter adapter = new ShopAdapter(BuyFoodActivity.this, shopList);
                        shopListView.setAdapter(adapter);

                        if(shopList.size()<20){
                            shopListView.hideFooterView();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }

            }
        });
    }

    public void chooseKouWei(View v){

        kouweiPopupWindow.showAsDropDown(v);

    }


    public void chooseNear(View v){

        nearPopupWindow.showAsDropDown(v);
    }


    private void initKouWeiPopupwindow(){
        View popupWindow_view = getLayoutInflater().inflate(
                R.layout.kouwei_popupwindow, null, false);
        kouweiPopupWindow = new PopupWindow(popupWindow_view,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, false);

        kouweiPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        kouweiPopupWindow.setFocusable(true);
        kouweiPopupWindow.setOutsideTouchable(true);

        GridView gridView = (GridView) popupWindow_view.findViewById(R.id.gridview);

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", "-99");
        map.put("name", "全部口味");

        kouWeiMapList.add(map);

        kouweiAdapter = new SimpleAdapter(this,kouWeiMapList,
                R.layout.kouwei_item, new String[]{"name"},
                new int[]{R.id.name});

        gridView.setAdapter(kouweiAdapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if(position == 0){
                    getShopList(null, null);
                }else{
                    getShopList(kouWeiList.get(position-1).getId(), null);
                }

                kouweiPopupWindow.dismiss();
            }
        });
    }


    private void initNearPopupwindow(){
        View popupWindow_view = getLayoutInflater().inflate(
                R.layout.near_popupwindow, null, false);
        nearPopupWindow = new PopupWindow(popupWindow_view,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, false);

        nearPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        nearPopupWindow.setFocusable(true);
        nearPopupWindow.setOutsideTouchable(true);

        nearGridView = (GridView) popupWindow_view.findViewById(R.id.gridview);

        List<HashMap<String, String>> nearMapList =  new ArrayList<HashMap<String, String>>();

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("name", "1000米");
        nearMapList.add(map);

        HashMap<String, String> map2 = new HashMap<String, String>();
        map2.put("name", "2000米");
        nearMapList.add(map2);

        HashMap<String, String> map3 = new HashMap<String, String>();
        map3.put("name", "5000米");
        nearMapList.add(map3);

        HashMap<String, String> map4 = new HashMap<String, String>();
        map4.put("name", "全城");
        nearMapList.add(map4);

        kouweiAdapter = new SimpleAdapter(this,nearMapList,
                R.layout.kouwei_item, new String[]{"name"},
                new int[]{R.id.name});

        nearGridView.setAdapter(kouweiAdapter);

        nearGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        getShopList(null, "1000");
                        break;
                    case 1:
                        getShopList(null, "2000");
                        break;
                    case 2:
                        getShopList(null, "5000");
                        break;
                    case 3:
                        getShopList(null, null);
                        break;
                    default:
                        break;
                }

                nearPopupWindow.dismiss();
            }
        });
    }


    @Override
    public void finish() {
//        unregisterReceiver(broadcastReceiver);
        super.finish();
    }
}
